home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 2.5 KB | 105 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 3.4 (Jacobi Iteration).
- % Section 3.7, Iterative Methods for Linear Systems, Page 186
- echo on; clc; format long; clear
-
- % This program solves a linear system AX = B.
-
- % Where A is diagonally dominant.
-
- % A is an n x n matrix, B is an n-dimensional vector.
-
- % The method is Jacobi fixed point iteration.
-
- % Remark. jacobi.m is used for Algorithm 3.4
-
- pause % Press any key to perform Jacobi iteration.
-
- clc;
- % Solve the system AX = B:
-
- A = [ 4 -1 1;
- 4 -8 1;
- -2 1 5];
-
- B = [ 7; -21; 15]; % Enter B as a column vector.
-
- pause % Press any key to continue.
-
- clc;
- % Place the starting vector in P
-
- % Place the tolerance in delta
-
- % Place the number of iterations in max1
-
- P = [ 1; 2; 2]; % Enter P as a column vector.
- delta = 1e-12;
- max1 = 50;
-
- [X,dX,Pm] = jacobi(A,B,P,delta,max1);
-
- pause % Press any key to continue.
-
- Mx1 = 'Computations for Jacobi iteration.';
- Mx2 = ' x y z';
- Mx3 = 'The matrix is A =';
- Mx4 = 'The vector B is displayed as B` =';
- Mx5 = 'The solution X is displayed as X` = ';
- Mx6 = 'The accuracy is +- dX, where dX` = ';
- clc,echo off,diary output,...
- disp(''),disp(Mx1),disp(Mx2),disp(Pm),...
- diary off,echo on
- pause % Press any key to continue.
- echo off,diary output,...
- disp(Mx3),disp(A),disp(Mx4),disp(B'),...
- disp(Mx5),disp(X'),disp(Mx6),disp(dX'),...
- disp('Iteration is converging linearly to the solution.'),...
- diary off,echo on
-
- pause % Press any key to perform Jacobi iteration.
-
- clc;
- % Solve the system:
-
- A = [-2 1 5;
- 4 -8 1;
- 4 -1 1];
-
- B = [15; -21; 7]; % Enter P as a column vector.
-
- pause % Press any key to continue.
-
- clc;
- % Place the starting vector in P
-
- % Place the tolerance in delta
-
- % Place the number of iterations in max1
-
-
- P = [ 1; 2; 2]; % Enter P as a column vector.
- delta = 1e-12;
- max1 = 4;
-
- [X,dX,Pm] = jacobi(A,B,P,delta,max1);
-
- pause % Press any key to continue.
-
- clc,echo off,diary output,...
- disp(''),disp(Mx1),disp(Mx2),disp(Pm),...
- diary off,echo on
- pause % Press any key to continue.
- echo off,diary output,...
- disp(Mx3),disp(A),disp(Mx4),disp(B'),...
- disp(Mx5),disp(X'),disp(Mx6),disp(dX'),...
- disp('Iteration is diverging to infinity.'),...
- disp('Notice the "scale factor" for the output.'),...
- diary off,echo on
-